
#include "Config.hpp"

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace com::toxiclabs::tsoc;
using namespace std;

Config::Config(string filename)
{
	/* default settings */
	shader_ao=false;
	shader_ao_n=8;
	shader_ao_factor=0.7f;
	
	camera_pos[0]=0.0f;
	camera_pos[1]=0.0f;
	camera_pos[2]=10.0f;
	camera_pos[3]=1.0f;
	
	
	ifstream fs;
	char buffer[128];
	
	cout<<"* reading config from:"<<filename<<endl;
	
	fs.open(filename.c_str());
	
	while(fs.good())
	{
		fs.getline(buffer,128);
		string tmp(buffer);
		
		size_t pos=tmp.find("=");
		if(pos!=string::npos)
		{
			string key=tmp.substr(0,pos);
			stringstream value(tmp.substr(pos+1,string::npos));
			
			if(key=="main.width")
			{
				value>>width;
				cout<<"-"<<key<<":"<<width<<endl;
			}
			if(key=="main.height")
			{
				value>>height;
				cout<<"-"<<key<<":"<<height<<endl;
			}
			if(key=="main.threads")
			{
				value>>num_threads;
				cout<<"-"<<key<<":"<<num_threads<<endl;
			}
			if(key=="main.filename")
			{
				value>>this->filename;
				cout<<"-"<<key<<":"<<this->filename<<endl;
			}
			
			if(key=="shader.ao")
			{
				value>>shader_ao;
				cout<<"-"<<key<<":"<<shader_ao<<endl;
			}
			
			if(key=="shader.ao_n")
			{
				value>>shader_ao_n;
				cout<<"-"<<key<<":"<<shader_ao_n<<endl;
			}
			
			if(key=="shader.ao_factor")
			{
				value>>shader_ao_factor;
				cout<<"-"<<key<<":"<<shader_ao_factor<<endl;
			}
			
			if(key=="camera.pos.x")
			{
				value>>camera_pos[0];
				cout<<"-"<<key<<":"<<camera_pos[0]<<endl;
			}
			
			if(key=="camera.pos.y")
			{
				value>>camera_pos[1];
				cout<<"-"<<key<<":"<<camera_pos[1]<<endl;
			}
			
			if(key=="camera.pos.z")
			{
				value>>camera_pos[2];
				cout<<"-"<<key<<":"<<camera_pos[2]<<endl;
			}
		}
		
	}
	
	fs.close();
	
}
